#ifndef __JNI__UTILS__H__
#define __JNI__UTILS__H__

#include <jni.h>
#include <asm/unistd.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <pthread.h>
#include <string>
#include "RendererCommon.h"

namespace simple_renderer{

JavaVM *GetJVM();
JNIEnv* GetEnv();
int InitRendererJvm(JavaVM* jvm);
std::string GetThreadId();
std::string GetThreadName();
JNIEnv* AttachCurrentThreadIfNeeded();
void InitAndroidContext(JNIEnv *env, jobject ctx);
jobject GetAndroidContext();

template <typename T>// T : jobject jarray ...
class JavaGlobalRef{
public:
    JavaGlobalRef(JNIEnv* env, T data){
        _data = static_cast<T>(env->NewGlobalRef(data));
        //_data = static_cast<T>(env->NewWeakGlobalRef(data));
    }
    ~JavaGlobalRef(){
        JNIEnv* env = AttachCurrentThreadIfNeeded();
        env->DeleteGlobalRef(_data);
        //env->DeleteWeakGlobalRef(_data);
    }

    T operator*() const {
        return _data;
    };

protected:
    T _data;
};

}
#endif